package com.hy.demo.ui.tools;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.gyf.immersionbar.BarHide;
import com.gyf.immersionbar.ImmersionBar;
import com.hy.demo.R;
import com.hy.demo.app.BaseActivity;
import com.hy.demo.util.NetWorkUtils;
import com.hy.demo.util.RootUtil;
import com.hy.frame.util.TimerUtil;
import com.hy.frame.util.ToastUtil;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * title AdbActivity
 * author HeYan
 * time 2022/8/17
 * desc 无
 */
public class AdbActivity extends BaseActivity implements View.OnClickListener {


    private TextView txtIP;
    private TextView txtMsg;

    @Override
    protected int getLayoutId() {
        return R.layout.v_tool_adb;
    }

    @Override
    protected void initView() {
        txtIP = findViewById(R.id.txt_ip);
        txtMsg = findViewById(R.id.txt_message);
        findViewById(R.id.btn_config).setOnClickListener(this);
        findViewById(R.id.btn_start).setOnClickListener(this);
        findViewById(R.id.btn_stop).setOnClickListener(this);
        findViewById(R.id.btn_notify).setOnClickListener(this);

    }

    @Override
    protected void initData() {
        setTitle(R.string.tools_adb);
        String ip = NetWorkUtils.getLocalIpAddress(this);
        txtIP.setText(String.format("本机IP：%s", ip));
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_config:
                exeCmd("setprop service.adb.tcp.port 5555");
                break;
            case R.id.btn_start:
                exeCmd("start adbd");
                break;

            case R.id.btn_stop:
                exeCmd("stop adbd");
                break;
            case R.id.btn_notify:
                startNotify();
                break;


            default:
                throw new IllegalStateException("Unexpected value: " + v.getId());
        }
    }

    private void exeCmd(String cmd) {
        boolean rst = RootUtil.exeCmdWithRoot(cmd);
        if (rst) {
            ToastUtil.show(this, "成功");
        } else {
            ToastUtil.show(this, "失败");
        }
    }

    private void startNotify(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                startNotifyIMPL();
            }
        }).start();
    }
    private void startNotifyIMPL(){
        WifiManager manager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifi = manager.getConnectionInfo();
        int ip = wifi.getIpAddress();

        int notifyIP = ip | 0xFF000000;

        DatagramSocket socket = null;
        try {
            String ipStr = NetWorkUtils.getBroadcastIP();
            //ipStr = "192.168.105.255";
            InetAddress server = InetAddress.getByName(ipStr);
            socket = new DatagramSocket();
            String data = "adb";
            DatagramPacket packet = new DatagramPacket(data.getBytes(), data.length(), server, 20441);
            socket.send(packet);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (socket != null) {
                socket.close();
            }
        }
    }
}
